﻿using Daemon.Model;
using Daemon.Common.Helpers;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Daemon.Common;
using System.Net;
using Daemon.Repository.EF.Enums;
using System;
using System.Linq;
using System.Collections.Generic;

namespace DaemonAPI
{
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class TakeAwayMoneySettingController : BaseApiController<TakeAwayMoneySetting, ITakeAwayMoneySettingRepository>
	{
		private readonly ITakeAwayShopCoordinateRepository _takeAwayShopCoordinateRepository;

		public TakeAwayMoneySettingController(ITakeAwayShopCoordinateRepository takeAwayShopCoordinateRepository, ITakeAwayMoneySettingRepository repository, IHttpContextAccessor httpContextAccessor) : base(repository, httpContextAccessor)
		{
			_takeAwayShopCoordinateRepository = takeAwayShopCoordinateRepository;
		}

		[HttpGet]
		[Route("")]
		public ResultModel GetMoneyByDistance(double lng, double lat, int shopId)
		{
			var shopInfo = _takeAwayShopCoordinateRepository.FindById(shopId);
			var distance = GeoHelper.CalculateDistance(lat, lng, Convert.ToDouble(shopInfo.Lat), Convert.ToDouble(shopInfo.Lng));
			var moneyInfo = _repository.FindAll().FirstOrDefault(r => r.MinDistance <= distance && r.MaxDistance >= distance);
			if (moneyInfo == null)
			{
				return new ResultModel(HttpStatusCode.NotFound, "超出配送范围！", null);
			}
			return new ResultModel(HttpStatusCode.OK, null, new List<decimal>() { moneyInfo.Money });
		}
	}
}
